<template>
  <div>
    <el-form @submit.native.prevent class="query-table-form" :inline="true" :model="params">
      <el-form-item>
        <el-input
          v-model="params.customer_name"
          placeholder="借款人/担保客户查询"
          clearable
          @change="getData()"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <select-address
          v-model="params.address_code"
          :auto-top="1"
          placeholder="地址查询"
          clearable
          @change="getData()"
        ></select-address>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="params.bank_name"
          placeholder="银行名称查询"
          clearable
          @keyup.enter.native="getData()"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="queryDate"
          type="month"
          placeholder="选择月"
          :picker-options="pickerOptions"
          @change="handleDateQuery()"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" @click="getData()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button
          icon="el-icon-download"
          type="primary"
          :loading="exportLoading"
          @click="exportExcel('十二大产业贷')"
        >
          导出
        </el-button>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-delete" type="primary" @click="handlePatchDelete()">
          批量删除
        </el-button>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-copy-document" type="primary" @click="handleRepeat()">
          项目去重
        </el-button>
      </el-form-item>
    </el-form>

    <c-table border v-loading="loading" class="table-sm" :columns="tableColumns" :data="tableData">
      <template v-slot:use="scope">
        <div class="text-truncate" :title="scope.row.funds_use_dir">
          {{ scope.row.funds_use_dir }}
        </div>
      </template>

      <template v-slot:mainBudiness="scope">
        <div class="text-truncate" :title="scope.row.main_business_bank">
          {{ scope.row.main_business_bank }}
        </div>
      </template>

      <template v-slot:reason="scope">
        <el-tooltip v-if="scope.row.ex_des">
          <div class="text-truncate text-danger">{{ scope.row.ex_des }}</div>
          <div slot="content" style="max-width: 300px">
            <span>{{ scope.row.ex_des }}</span>
          </div>
        </el-tooltip>
        <span v-else>-</span>
      </template>
    </c-table>

    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="tableCount"
        @current-change="getData"
      ></el-pagination>
    </div>

    <el-dialog
      append-to-body
      top="30px"
      width="1100px"
      :close-on-click-modal="false"
      :visible.sync="oneIndustryVisible"
    >
      <industry-credit :one-industry="oneIndustry"></industry-credit>
    </el-dialog>

    <project-new-dialog
      :visible.sync="formVisible"
      :is-have-template="isHaveTemplate"
      :table-id="tableId"
      :form-data="formData"
      @success="handleSuccess"
    ></project-new-dialog>

    <project-deletes :visible.sync="deleteVisible" @success="handleSuccess()"></project-deletes>

    <project-repeat :visible.sync="repeatVisible" @success="handleSuccess()"></project-repeat>
  </div>
</template>
<script>
import ProjectNewDialog from '@/bank/views/hnd/ProjectNewDialog'
import IndustryCredit from '@/bank/components/IndustryCredit'
import SelectAddress from '@/components/select/SelectAddress'
import { pickerOptions } from '@/bank/views/hnd/RecommendPass'
import hndTableMixin from '@/bank/mixin/hndTable'
import { mapGetters } from 'vuex'
import ProjectDeletes from '@/bank/views/hnd/ProjectDeletes'
import ProjectRepeat from '@/bank/views/hnd/ProjectRepeat'
export const columns = []
export default {
  components: {
    ProjectNewDialog,
    IndustryCredit,
    SelectAddress,
    ProjectDeletes,
    ProjectRepeat,
  },
  mixins: [hndTableMixin],
  props: {},
  data() {
    return {
      pickerOptions,
      params: {
        index: 1,
        size: 10,
        'import_status.in': '2,3',
        'is_deleted.neq': 1,
        customer_name: '',
        'bank_code.endlike': '',
      },
      order: {
        create_time: 'desc',
      },
      queryDate: '',
      tableName: 'datum_one_county_industry',
      tableColumns: [
        { attrs: { label: '借款人/担保客户', prop: 'customer_name', minWidth: '120px' } },
        { attrs: { label: '客户类型', prop: 'org_class_name', align: 'center' } },
        { attrs: { label: '导入银行', prop: 'bank_name', align: 'center' } },
        { attrs: { label: '放款日/拟担保起始日', prop: 'loadDate', align: 'center' } },
        { attrs: { label: '到期日', prop: 'loadEndDate', align: 'center' } },
        { attrs: { label: '贷款期限(月)', prop: 'warranty_period', align: 'center' } },
        { attrs: { label: '联系电话', prop: 'corp_phone', align: 'center' } },
        { attrs: { label: '贷款金额/拟担保金额(万元)', prop: 'warranty_amount', align: 'center' } },
        { attrs: { label: '借款用途', prop: 'funds_use_dir', align: 'center' }, slot: 'use' },
        {
          attrs: { label: '异常原因', align: 'center', prop: 'not_pass_reason', width: '120px' },
          slot: 'reason',
        },
        { attrs: { label: '导入时间', prop: 'create_time', align: 'center' } },
        {
          attrs: { label: '操作', align: 'center', width: '110px' },
          operations: [
            {
              content: '修改',
              icon: 'fa fa-edit',
              attrs: { class: 'py-0 px-0' },
              event: this.handleToEdit,
              ifRender: row => +row.import_status !== 3,
            },
            { content: '详情', icon: 'fa fa-search', event: this.showCreditFile },
            // { content: '删除', icon: 'fa fa-trash', attrs: { 'class': 'text-danger py-0 px-0' }, event: this.handleDelete } //真的删除
            {
              content: '删除',
              icon: 'fa fa-trash',
              attrs: { class: 'text-danger py-0 px-0' },
              event: this.handleUpdate,
            }, // 改状态
          ],
        },
      ],

      // 修改
      formTable: 'datum_one_county_industry',
      formVisible: false,
      formData: {},
      tableId: '',

      // 详情
      oneIndustry: {},
      oneIndustryVisible: false,
      deleteVisible: false,
      repeatVisible: false,
      isHaveTemplate: false,
    }
  },
  computed: {
    ...mapGetters(['user', 'org']),
    bankName() {
      return this.org.orgName || '-'
    },
    bankCode() {
      return this.org.orgCode
    },
  },
  watch: {},
  created() {
    this.params['bank_code.endlike'] = this.org.orgCode
    this.getData()
    this.getTemplate()
  },
  methods: {
    getParams() {
      return this.params
    },
    async getTemplate() {
      this.loading = true
      const data = await this.$store.dispatch('getImportTemplate')
      this.loading = false
      this.isHaveTemplate = !!data.length
      if (this.isHaveTemplate) {
        const column = {
          attrs: {
            label: '行业类型',
            prop: 'main_business_bank',
            align: 'center',
            minWidth: '110px',
          },
        }
        this.tableColumns.splice(3, 0, column)
      }
    },
    handlePatchDelete() {
      this.deleteVisible = true
    },
    handleRepeat() {
      this.repeatVisible = true
    },
    handleDateQuery() {
      const startTime = this.queryDate
        ? `${this.queryDate.getFullYear()}-${this.queryDate.getMonth() + 1}-1`
        : ''
      const endTime = this.queryDate
        ? `${this.queryDate.getFullYear()}-${this.queryDate.getMonth() + 2}-1`
        : ''
      this.params['create_time.gte'] = startTime
      this.params['create_time.lt'] = endTime
      this.getData()
    },
    dataAdapter(item) {
      item.loadDate = this.$utils.dateFormat(item.load_date, 'yyyy-MM-dd')
      item.loadEndDate = this.$utils.dateFormat(item.load_end_date, 'yyyy-MM-dd')

      return item
    },
    handleDelete(row = {}) {
      this.$utils
        .msgbox({
          message: `确定删除担保客户：${row.customer_name}?`,
          beforeClose: () =>
            this.$api.deleteData(this.$serviceName, this.formTable, row.customer_related_id),
        })
        .then(() => {
          this.$message.success('删除成功！')
          this.handleSuccess()
        })
        .catch(() => this.$message.error('删除失败，请重试！'))
    },

    handleUpdate(row = {}) {
      this.$utils
        .msgbox({
          message: `确定删除担保客户：${row.customer_name}?`,
          beforeClose: () =>
            this.$api.updateData(this.$serviceName, this.formTable, row.customer_related_id, {
              is_deleted: 1,
            }),
        })
        .then(() => {
          this.$message.success('删除成功！')
          this.handleSuccess()
        })
        .catch(() => this.$message.error('删除失败，请重试！'))
    },

    handleToEdit(row = {}) {
      this.formData = row
      this.formData.warranty_period = (row.warranty_period || '').replace(/[^0-9]/g, '')
      this.tableId = this.formData['customer_related_id'] || ''
      this.formVisible = true
    },
    showCreditFile(row = {}) {
      this.oneIndustry = row
      this.oneIndustryVisible = true
    },
    handleSuccess() {
      this.$store.dispatch('getTaskCountHnd')
      this.getData(this.params.index)
    },
  },
}
</script>
<style lang="scss" scoped></style>
